package top.jiangqiang.qyblog.common.markdown.link;

import cn.hutool.core.util.StrUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatchUrl {
    private final static Pattern TENCENT_PATTERN = Pattern.compile("j_vid=([^&]+)");
    private final static Pattern YOUKU_PATTERN = Pattern.compile("//v\\.youku\\.com/v_show/id_(\\w+)=*\\.html");
    private final static Pattern BILIBILI_PATTERN = Pattern.compile("//www\\.bilibili\\.com/video/([\\w-]+)");


    public static String matchTencentVideo(String srcUrl) {
        if (StrUtil.isBlank(srcUrl)) {
            return "";
        }
        //腾讯视频
        if (srcUrl.contains("//v.qq.com/")) {
            //https://v.qq.com/x/cover/mzc00200orkn5n8.html?j_vid=v3522q7k3fx&j_cut_vid=&j_is_win_vid=1&j_start=0
            Matcher matcher = TENCENT_PATTERN.matcher(srcUrl);
            if (matcher.find()) {
                return "https://v.qq.com/txp/iframe/player.html?vid=" + matcher.group(1);
            }
        } else if (srcUrl.contains("//v.youku.com/")) {
            //https://v.youku.com/v_show/id_XMzk1NjM1MjAw.html?spm=a2hja.14919748_WEBHOME_NEW.drawer2.d_zj1_2&s=cc003400962411de83b1&scm=20140719.rcmd.35027.show_cc003400962411de83b1
            Matcher matcher = YOUKU_PATTERN.matcher(srcUrl);
            if (matcher.find()) {
                String videoId = matcher.group(1);
                return "//player.youku.com/embed/" + videoId;
            }
        } else if (srcUrl.contains("//www.bilibili.com/")) {
            //https://www.bilibili.com/video/BV1e94y1B7ju/?spm_id_from=333.1007.tianma.2-1-3.click
            Matcher matcher = BILIBILI_PATTERN.matcher(srcUrl);
            if (matcher.find()) {
                String videoId = matcher.group(1);
                return "//player.bilibili.com/player.html?bvid=" + videoId;
            }
        }
        return srcUrl;
    }

}
